package com.nsu.cl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nsu.cl.pojo.LoginForm;
import com.nsu.cl.pojo.Student;
import com.nsu.cl.service.StudentService;
import com.nsu.cl.mapper.StudentMapper;
import com.nsu.cl.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 52618
* @description 针对表【student(学生表)】的数据库操作Service实现
* @createDate 2023-02-16 14:21:02
*/
@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Override
    public Student login(LoginForm loginForm) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));

        Student student = baseMapper.selectOne(queryWrapper);

        return student;
    }

    @Override
    public Student getStudentById(Long userId) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<Student> getStudentByOpr(Page<Student> pageParam, Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(student.getName())){
            queryWrapper.like("name",student.getName());
        }
        if(!StringUtils.isEmpty(student.getClazzName())){
            queryWrapper.like("clazz_name",student.getClazzName());
        }
        queryWrapper.orderByAsc("id");

        Page<Student> page = baseMapper.selectPage(pageParam, queryWrapper);

        return page;
    }

    @Override
    public List<Student> getStudentBySome(Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(student.getName())){
            queryWrapper.eq("name",student.getName());
        }
        if(!StringUtils.isEmpty(student.getClazzName())){
            queryWrapper.eq("clazz_name",student.getClazzName());
        }
        if(!StringUtils.isEmpty(student.getId())){
            queryWrapper.eq("id",student.getClazzName());
        }
        queryWrapper.orderByAsc("id");

        List<Student> studentList = baseMapper.selectList(queryWrapper);

        return studentList;
    }
}




